package com.syh.bishe.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.syh.bishe.Util.RedisConstants;
import com.syh.bishe.pojo.Log;
import com.syh.bishe.mapper.LogMapper;
import com.syh.bishe.service.ILogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author syh
 * @since 2024-07-06
 */
@Service
@RequiredArgsConstructor
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements ILogService {
    private final LogMapper logMapper;
    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public List<Log> getSome(int userId) {
        String s = stringRedisTemplate.opsForValue().get(RedisConstants.MAIN_ACT_DATA + userId);
        if(StrUtil.isNotBlank(s)){
            return JSONUtil.toList(s, Log.class);
        }
        List<Log> logs = logMapper.getSome(userId);
        stringRedisTemplate.opsForValue().set(RedisConstants.MAIN_ACT_DATA + userId, JSONUtil.toJsonStr(logs),
                RedisConstants.CACHE_ACT_TTL, TimeUnit.SECONDS);
        return logs;
    }
}
